package com.xyh.flowershop.exceptions.handles;

import com.xyh.flowershop.constant.CommonConstant;
import com.xyh.flowershop.exceptions.custom.AuthException;
import com.xyh.flowershop.exceptions.custom.BizException;
import com.xyh.flowershop.exceptions.custom.StockException;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 小约翰可汗
 * @className ExceptionHandler
 * @createTime 2024/5/29:47
 */
@RestControllerAdvice
public class ExceptionHandlers {

    @ExceptionHandler(AuthException.class)
    public ResultRespVo handleAuthException(AuthException exception){
        String msgCode = exception.getMessage();
        return switch (msgCode) {
            case CommonConstant.FORBIDDEN -> ResultRespVo.error(Integer.parseInt(msgCode), "暂无权限访问");
            case CommonConstant.UNAUTHORIZED -> ResultRespVo.error(Integer.parseInt(msgCode), "鉴权失败，请重新登录");
            default -> ResultRespVo.error();
        };
    }

    @ExceptionHandler(StockException.class)
    public ResultRespVo handleStockException(StockException exception){
        String msgCode = exception.getMessage();
        return ResultRespVo.error(Integer.parseInt(msgCode), "库存不足");
    }

    @ExceptionHandler(BizException.class)
    public ResultRespVo handleBizException(BizException exception){
        String msgCode = exception.getMessage();
        return ResultRespVo.error(Integer.parseInt(CommonConstant.FAIL), exception.getMessage());
    }

}
